<template>
  <div class="message">
    <div class="commons_top">
      <div class="commons_back" @click="$router.go(-1)">
        <img src="@/assets/index/back.png" />
      </div>
      <span class="commons_title">系统通知</span>
      <span class="commons_cancel" @click="batchDel">清空全部</span>
    </div>
    <div class="message_wrap" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading" infinite-scroll-distance="20">
      <div class="message_list" :class="item.isTouchMove ? 'touch-move-active' : ''"
       v-for="item in messageList" :key="item.index" @click="linkDet(item.id)">
        <van-swipe-cell>
          <div class="message_main">
            <img src="http://192.168.2.247/file/messageICon/icon_mess.png" class="icon_mess">
            <div class="message_con">
              <div class="message_name">
                <span class="circle" v-if="item.status == 0"></span>
                <span class="name">{{item.name}}</span>
                <span class="date">{{item.sendTime.substring(0,16)}}</span>
              </div>
              <div class="message_info">{{item.content}}</div>
            </div>
          </div>
          <template #right>
            <van-button square text="删除" type="danger" class="delete-button" @click="del(item.id)" />
          </template>
        </van-swipe-cell>
      </div>
      <p v-if="loading" class="page-infinite-loading">
        <span>
          <mt-spinner type="fading-circle"></mt-spinner>
          加载中...
        </span> 
      </p>
      <p v-else class="loadOver">
        <span v-if="!isNull && isLast">—— 我是有底线的 ——</span>
      </p>
    </div>
    <div class="special_no" v-if="messageList && messageList.length == 0 && !loading">
      <img class="no_con" src="@/assets/common/no_content.png">
      <div class="no_text">抱歉，目前暂无内容哦~</div>
    </div>
  </div>
</template>

<script>
import '@static/css/common.css'
import { Toast } from 'mint-ui';
import { getListAPI, deleAPI, batchDelAPI } from '@/api/message'

export default {
  data() {
    return {
      messageList: [],
      totalPage: null,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      loading: false,
      isNull: '',
      isLast: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getListAPI(this.listQuery).then(res => {
        if(res.data.code === 0) {
          var list = []
          if(this.listQuery.pageNum == 1) { //第一页
            list = res.data.data.list
          } else {
            list = this.messageList.concat(res.data.data.list)
          }
          if(list){
            list.forEach((res, idx) => {
              list[idx].isTouchMove = false
            })
          }
          this.messageList = list
          this.totalPage = res.data.data.pages
          this.isNull = list && list.length > 0 ? false : true
          this.isLast = res.data.data.pages == this.listQuery.pageNum ? true : false
        } else {
          Toast(res.data.msg)
        }
        this.loading = false
      })
    },
    loadMore() {
      if(this.totalPage > this.listQuery.pageNum) {
        this.listQuery.pageNum = this.listQuery.pageNum + 1
        this.getList()
      }
    },
    del(id) { //删除
      let that = this
      this.$confirm('确认要删除当前消息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true
      }).then(() => {
        deleAPI(id).then(res => {
          if(res.data.code==0){
            Toast('删除成功')
            that.getList()
          }else{
            Toast(res.data.msg)
          }
        })
      })
    },
    batchDel() { //清空全部消息
      let that = this
      this.$confirm('确认清空全部消息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true
      }).then(() => {
        batchDelAPI().then(res => {
          if(res.data.code==0){
            Toast('删除成功')
            that.getList()
          }else{
            Toast(res.data.msg)
          }
        })
      })
    },
    linkDet(id) { //跳转至详情页
      this.navigationFuc('messageDetails',{ id })
    }
  },
}
</script>

<style>
.mint-spinner-fading-circle{
  margin: 0 auto;
  margin-bottom: 10px;
}
.page-infinite-loading{
  text-align: center;
  padding: 10px;
  color: #999999;
  font-size: 14px;
}
.message .loadOver{
  height: 0.8rem;
  text-align: center;
  line-height: 0.8rem;
  color: #999999;
  font-size: 14px;
}
.message_list .delete-button {
  height: 100% !important;
}
.el-message-box {
  width: 250px !important;
}
</style>
<style scoped>
.message {
  width: 100%;
  min-height: 100vh;
  background: #F8F8F8;
}
.commons_cancel {
  margin: auto 0 auto 23%;
  font-size: 14px;
  color: #666666;
}
.message .commons_top {
  background: #ffffff;
}
.message_wrap {
  width: 100%;
  padding-top: 0.09rem;
}
.message_list {
  border-bottom: 0.02rem solid #EEEEEE;
  background: #ffffff;
}
.message_main {
  width: 0;
  display: flex;
  padding: 0.3rem;
  box-sizing: border-box;
}
.icon_mess {
  width: 0.68rem;
  height: 0.68rem;
  margin-right: 0.23rem;
  margin-top: 0.1rem;
}
.message_con {
  flex: 1;
}
.message_name {
  overflow: hidden;
  margin-bottom: 0.2rem;
}
.message_name .circle {
  display: block;
  width: 0.1rem;
  height: 0.1rem;
  background: #f56c6c;
  border-radius: 50%;
  margin-right: 0.06rem;
  float: left;
  margin-top: 0.14rem;
}
.message_name .name {
  color: #333333;
  font-size: 14px;
  overflow: hidden;
  display: inline-block;
  white-space: nowrap;
  text-overflow:ellipsis;
  width: 3.6rem;
}
.message_name .date {
  float: right;
  color: #B1B1C3;
  font-size: 10px;
  margin-top: 0.08rem;
}
.message_info {
  font-size: 14px;
  color: #878B93;
  width: 6rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow:ellipsis;
}
</style>